package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * SpringAMQP写法：消费者
 * 启动ConsumerApplication,就能接收到消息
 */
@Component
public class AmqpConsumerListener {

    /**
     * 1.HelloWorld
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue_HelloWorld(String msg) throws InterruptedException{
        System.out.println("Listener1接收的消息是：【"+msg+"】");
        Thread.sleep(20);
    }

    /**
     * 2.work模式,看两个listener分别接收到多少条消息
     * 在application.yml中配置了prefetch = 1,这里休眠了100毫秒,所有处理能力弱些,处理的数据也应少些
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue_Work(String msg) throws InterruptedException{
        System.out.println("Listener2接收的消息是：【"+msg+"】");
        Thread.sleep(100);
    }

    /**
     *
     * 3-1.fanout模式
     * fanout.queue1和fanout.exchange绑定了
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) throws InterruptedException{
        System.out.println("fanout.queue1接收的消息是：【"+msg+"】");
    }

    /**
     * 3-2.fanout模式
     * fanout.queue2和fanout.exchange绑定了
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) throws InterruptedException{
        System.out.println("fanout.queue2接收的消息是：【"+msg+"】");
    }

    /**
     * 4-1.direct模式：使用注解的方式声明：队列、交换机、路由key,并将队列和交换机进行绑定
     * direct.queue1和direct.exchange绑定了
     * 路由key为："key1","key2"
     * @param msg
     */
    @RabbitListener(
        bindings = @QueueBinding(//关系绑定
        value = @Queue(name = "direct.queue1"),//声明队列
        exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),//声明direct类型的交换机
        key = {"key1","key2"}//路由key
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("direct.queue1接收的消息是：【"+msg+"】");
    }

    /**
     * 4-2.direct模式：使用注解的方式声明：队列、交换机、路由key,并将队列和交换机进行绑定
     * direct.queue2和direct.exchange绑定了
     * 路由key为："key1","key3"
     * @param msg
     */
    @RabbitListener(
        bindings = @QueueBinding(//关系绑定
        value = @Queue(name = "direct.queue2"),//声明队列
        exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),//声明direct类型的交换机
        key = {"key1","key3"}//路由key
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("direct.queue2接收的消息是：【"+msg+"】");
    }

    /**
     * 5-1.topic模式：使用注解的方式声明：队列、交换机、路由key,并将队列和交换机进行绑定
     * topic.queue1和topic.exchange绑定了
     * 路由key为："china.#","china.*"
     * #：代表0个或多个单词
     * *：代表一个单词
     * @param msg
     */
    @RabbitListener(
        bindings = @QueueBinding(//关系绑定
        value = @Queue(name = "topic.queue1"),//声明队列
        exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),//声明topic类型的交换机
        key = {"china.#"}//路由key
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("topic.queue1接收的消息是：【"+msg+"】");
    }

    /**
     * 5-2.topic模式：使用注解的方式声明：队列、交换机、路由key,并将队列和交换机进行绑定
     * topic.queue1和topic.exchange绑定了
     * 路由key为："china.#","china.*"
     * #：代表0个或多个单词
     * *：代表一个单词
     * @param msg
     */
    @RabbitListener(
        bindings = @QueueBinding(//关系绑定
        value = @Queue(name = "topic.queue2"),//声明队列
        exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),//声明topic类型的交换机
        key = {"china.*"}//路由key
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("topic.queue2接收的消息是：【"+msg+"】");
    }

    /**
     * 6.测试接受object类型数据
     */
    @RabbitListener(
        bindings = @QueueBinding(//关系绑定
        value = @Queue(name = "object.queue"),//声明队列
        exchange = @Exchange(name = "object.exchange",type = ExchangeTypes.FANOUT)//声明fanout类型的交换机
    ))
    public void listenObjectQueue(Map<String,Object> msg) throws InterruptedException{
        System.out.println("object.queue接收的消息是：【"+msg+"】");
    }
}
